	*Only works in the main do file

		

	
	use "$data/Global_Admin1_Data.dta", replace

	
	
	merge m:1 PovDecile using "$data/ForProjections_Admin1.dta", nogen
	
	
		
	
	
	
	duplicates drop state_id year PovDecile, force
	
	
	bysort state_id year: egen avg_days=mean(days90plus_dry)
		label var avg_days "Average days annually above 90F 2041-2050"
	
	
	
	
	bysort state_id: egen avg_days_formap=mean(days90plus_dry)
	
	
		
	
	levelsof iso, local(levels)
	
	foreach i of local levels{
		g avg_days_poor`i'=avg_days if PovDecile>=10 & iso=="`i'"
			label var avg_days_poor`i' "Poor"
		g avg_days_rich`i'=avg_days if PovDecile<=1 & iso=="`i'"
			label var avg_days_rich`i' "Rich"
			
		
	}
	
	
	
	
	
	levelsof iso, local(levels)
	

		foreach i of local levels{
			preserve
				keep if iso=="`i'"
				collapse (mean) avg_days_rich`i' avg_days_poor`i' , by(iso)
				
				g diff_days_`i'=avg_days_rich`i'-avg_days_poor`i' 
					sum diff_days_`i'
					local days`i' : di %9.1fc `r(mean)'
					sum avg_days_rich`i'
					local daysrich`i' : di %9.1fc `r(mean)'
					sum avg_days_poor`i'
					local dayspoor`i' : di %9.1fc `r(mean)'
				
			
					
			restore	

		}
	
		
	
	
	texdoc init "$output/table2.tex", replace  force
		tex &\multicolumn{1}{c}{\shortstack{Difference}}&\multicolumn{1}{c}{\shortstack{Days\\\$>90\degree\$F, wealthy}}&\multicolumn{1}{c}{\shortstack{Days\\\$>90\degree\$F, poor}} \\
		tex \midrule
		tex India & `daysInd' & `daysrichInd' & `dayspoorInd'  \\
		tex Mexico & `daysMex' & `daysrichMex' & `dayspoorMex'  \\
		tex Morocco & `daysMor' & `daysrichMor' & `dayspoorMor'  \\
		tex Mozambique & `daysMoz' & `daysrichMoz' & `dayspoorMoz' \\
		tex Nigeria & `daysNig' & `daysrichNig' & `dayspoorNig'  \\
		tex \midrule
	texdoc close
	                  
	clear
	
	
	
	
	
	use "$data/Global_Admin2_Data.dta", replace

	
	
	merge m:1 PovCentile using "$data/ForProjections_Admin2.dta", nogen
	
	
	
	bysort state_id year: egen avg_days=mean(days90plus_dry)
		label var avg_days "Average days annually above 90F 2041-2050"
	
	
	
	
	bysort state_id: egen avg_days_formap=mean(days90plus_dry)
	
	
	
		
	
	levelsof iso, local(levels)
	
	foreach i of local levels{
		g avg_days_poor`i'=avg_days if PovCentile>=10 & iso=="`i'"
			label var avg_days_poor`i' "Poor"
		g avg_days_rich`i'=avg_days if PovCentile<=1 & iso=="`i'"
			label var avg_days_rich`i' "Rich"
			
	
	}
	
	
	
	
	
	levelsof iso, local(levels)
	

	foreach i of local levels{
		preserve
			keep if iso=="`i'"
			collapse (mean) avg_days_rich`i' avg_days_poor`i' , by(iso)
			
			g diff_days_`i'=avg_days_rich`i'-avg_days_poor`i' 
				sum diff_days_`i'
				local days`i' : di %9.1fc `r(mean)'
				sum avg_days_rich`i'
				local daysrich`i' : di %9.1fc `r(mean)'
				sum avg_days_poor`i'
				local dayspoor`i' : di %9.1fc `r(mean)'
			
		
			
		restore	

	}
	
		
	
	
	texdoc init "$output/table2.tex", append force
		tex Indonesia, P2 & `daysidn' & `daysrichidn' & `dayspooridn'  \\
		tex \midrule
	texdoc close
	                  
	

	
	preserve
		keep if avg_days_formap!=.
		duplicates drop state_id, force
		keep state_id PovCentile avg_days_formap
		export delimited using "$output/EffectMapping.csv", replace
	restore	
	
	

	
